
//相交链表
  class ListNode {
      int val;
      ListNode next;
      ListNode(int x) {
          val = x;
          next = null;
      }
  }

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA==null){
            return null;
        }
        if(headB==null){
            return null;
        }
        ListNode cur=headA;
        int lenA=0;
        while(cur!=null){
            lenA++;
            cur=cur.next;
        }
        ListNode cur1=headB;
        int lenB=0;
        while(cur1!=null){
            lenB++;
            cur1=cur1.next;
        }
        int len=lenA>lenB?(lenA-lenB):(lenB-lenA);
        if(lenA>lenB){

            while(len!=0){
                headA=headA.next;
                len--;
            }
            while(headB!=headA){
                headB=headB.next;
                headA=headA.next;
            }
            return headA;
        }else{
            while(len!=0){
                headB=headB.next;
                len--;
            }
            while(headA!=headB){
                headA=headA.next;
                headB=headB.next;
            }
            return headA;
        }
    }
}


//方法二
/*

  public class ListNode {
      int val;
      ListNode next;
      ListNode(int x) {
          val = x;
          next = null;
      }
  }

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA==null){
            return null;
        }
        if(headB==null){
            return null;
        }
        ListNode cur=headA;
        ListNode temp=headB;
        int lenA=0;
        int lenB=0;
        while(cur!=null){
            lenA++;
            cur=cur.next;
        }
        while(temp!=null){
            lenB++;
            temp=temp.next;
        }

        int len=lenA-lenB;
        //cur为长
        cur=headA;
        //temp为短
        temp=headB;

        if(len<0){
            cur=headB;
            temp=headA;
            len=lenB-lenA;
        }

        //长的先走len步
        while(len!=0){
            cur=cur.next;
            len--;
        }

        while(cur!=temp){
            cur=cur.next;
            temp=temp.next;
        }
        return cur;
    }
}*/